<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\Request;

class Chat extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 文本消息的数据持久化
     *
     */
    public function saveMessage()
    {
        if (Request::instance()->isAjax()){
            $message = input('post.');

            $datas['fromid']    = $message['fromid'];
            $datas['fromname']  = $this->getName($message['fromid']);
            $datas['toid']      = $message['toid'];
            $datas['toname']    = $this->getName($message['toid']);
            $datas['content']   = $message['data'];
            $datas['time']      = $message['time'];
            $datas['is_read']   = $message['is_read'];
            $datas['type']      = 1;  // 文本1 图片2
            $datas['create_time']= date('Y-m-d H:i:s');

            $res = Db::table('chat_communication')->insert($datas);
            return $res;
        }

        return'请求方式异常';
    }

    /**
     * 根据用户id 返回姓名
     */
    public function getName($uid=1)
    {
        $userInfo = Db::name('user')
            ->where('id','=',$uid)
            ->find();

        return $userInfo['nickname'];
    }


}
